package src;


import java.util.Random;

import src.token.Token;




public class Level {

	//La representacion del nivel es un array de tokens: null -> no hay elemento en esa posicion.
	public static Token[][] levelData = null;
	
	//public static int level_size = 0;
	
	public static int level_number = 1;
	
	//Dato necesario para saber donde termina la fila de la matriz:
	public static int colums = Ks.columnas;
	
	
	public static void init(){
		
	
		
		levelData = new Token[Ks.columnas][Ks.filas];
		//level_size = Ks.filas * Ks.columnas;
		
		//TODO: leer archivo de datos (quizas un xml o un txt pedorro)
		//POR AHORA SE GENERA RAMDOMICAMENTE:
		loadData();
		
	}
	
	public static void loadData(){
		Random r = new Random();
		int posX;
		int posY;
		
		
		for (int j = 0; j < Ks.filas; j++){
			for (int i = 0; i < Ks.columnas; i++){
				posX = i * Ks.token_size + Ks.LEVEL_X;
				posY = j * Ks.token_size + Ks.LEVEL_Y;
				
				if (r.nextInt() % 2 == 0){
					levelData[i][j] = new Token(i, j,posX,posY,(Math.max(r.nextInt(6),1)));
					
				}

			}
			
		}
		
	
		
		if (Ks.DBG && Ks.DBG_L){
			for (int i = 0; i < Ks.columnas; i++){
				for (int j = 0; j < Ks.filas; j++){
					System.out.print("pos i:_"+ i + ((levelData[i] == null)? "null" : "no null" ));
					if (levelData[i][j] != null) System.out.println(" pos x: " + levelData[i][j].pos_x + " pos y: " + levelData[i][j].pos_y + " level: " + levelData[i][j].level);
					else System.out.println(" ");
				}
			}
		}
	}
		
	
	
	public static void checkChain(){
	

		
		for (int j = 0 ; j < Ks.filas ; j++){
			for (int i = 0; i < Ks.columnas; i++){
			
				
				if (i + 1 < Ks.columnas){
				
					if (levelData[i][j] != null && levelData[i + 1][j] != null){
					
						if (levelData[i][j].level == levelData[i + 1][j].level){
								levelData[i][j].m_next = levelData[i + 1][j];
						}
					}
				}
				
				if (i - 1 > 0){
			
					if (levelData[i][j] != null && levelData[i - 1][j] != null){
						if (levelData[i][j].level == levelData[i - 1][j].level){
								levelData[i][j].m_prev = levelData[i - 1][j];
						}
					}
				}
			}
		}
		
		
	}
	
	public static void UpdateChains(){
		
	
		checkChain();
		
	}

	public static int posXLevelInitial = 0;

	public static int posYLevelInitial = 0;
	

	
	

	
	public static Token getToken(int x, int y){
		Token t = null;
		
		//return levelData[(int)x - Ks.LEVEL_X/ Ks.token_size ][(int)(y - Ks.LEVEL_Y)/ Ks.token_size];
		
		for (int i = 0; i < Ks.columnas; i++){
			for (int j = 0; j < Ks.filas; j++){
					if (levelData[i][j] != null){
						if (levelData[i][j].pos_x <= x && (x <= levelData[i][j].pos_x + Ks.token_size) &&
								( levelData[i][j].pos_y <= y) && y <= (levelData[i][j].pos_y + Ks.token_size)){
							return levelData[i][j];
					}
				}
			}
		}
		
		return t;
	}
	

	public static boolean changeTokenPosition(Token t, int x, int y){
		boolean changed = false;
		
		//ya habia un token en esa posicion, asi que no cambio nada.
		if (getToken(x,y) != null){
			return false;
		}
		int posX;
		int posY;
		
		for (int i = 0; i < Ks.columnas; i++){
			for (int j = 0; j < Ks.filas; j++){
					
				posX = i * Ks.token_size + Ks.LEVEL_X;
				posY = j * Ks.token_size + Ks.LEVEL_Y;
				
				if (posX <= x && (x <= posX + Ks.token_size) &&
								( posY <= y) && y <= (posY + Ks.token_size)){
					
					t.pos_x = posX;
					t.pos_y = posY;
					
					levelData[i][j] = t;
					
					levelData[t.pos_i][t.pos_j] = null;
					
					t.pos_i = i;
					t.pos_j = j;
					
					if (t.m_next != null){
						t.m_next.m_prev = null;
						t.m_next = null;
					}
					
					if(t.m_prev != null){
						t.m_prev.m_next = null;
						t.m_prev = null;
					}
					
					
					
				}
			}
		}
		
		
		return changed;
	}
	

}
